package com.ruoyi.finance.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.finance.domain.SubjectType;
import com.ruoyi.finance.mapper.SubjectTypeMapper;
import com.ruoyi.finance.vo.SubJectDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.SubjectMapper;
import com.ruoyi.finance.domain.Subject;
import com.ruoyi.finance.service.ISubjectService;

/**
 * 会计科目Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService
{
    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SubjectTypeMapper subjectTypeMapper;

    /**
     * 查询会计科目
     *
     * @param subjectId 会计科目主键
     * @return 会计科目
     */
    @Override
    public Subject selectSubjectBySubjectId(Long subjectId)
    {
        return subjectMapper.selectSubjectBySubjectId(subjectId);
    }

    /**
     * 查询会计科目列表
     *
     * @param subject 会计科目
     * @return 会计科目
     */
    @Override
    public List<Subject> selectSubjectList(Subject subject)
    {
        return subjectMapper.selectSubjectList(subject);
    }

    /**
     * 新增会计科目
     *
     * @param subject 会计科目
     * @return 结果
     */
    @Override
    public int insertSubject(Subject subject)
    {
        return subjectMapper.insertSubject(subject);
    }

    /**
     * 修改会计科目
     *
     * @param subject 会计科目
     * @return 结果
     */
    @Override
    public int updateSubject(Subject subject)
    {
        return subjectMapper.updateSubject(subject);
    }

    /**
     * 批量删除会计科目
     *
     * @param subjectIds 需要删除的会计科目主键
     * @return 结果
     */
    @Override
    public int deleteSubjectBySubjectIds(Long[] subjectIds)
    {
        return subjectMapper.deleteSubjectBySubjectIds(subjectIds);
    }

    /**
     * 删除会计科目信息
     *
     * @param subjectId 会计科目主键
     * @return 结果
     */
    @Override
    public int deleteSubjectBySubjectId(Long subjectId)
    {
        return subjectMapper.deleteSubjectBySubjectId(subjectId);
    }

    @Override
    public List<SubJectDto> tosubjectlist() {
        List<SubjectType> subjectTypes = subjectTypeMapper.selectList(null);
        return subjectTypes.stream()
                .map(subjectType -> {
                    SubJectDto subJectDto = new SubJectDto();
                    subJectDto.setTypeId(subjectType.getTypeId());
                    subJectDto.setTypeName(subjectType.getTypeName());
                    QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
                    subjectQueryWrapper.eq("type_id", subjectType.getTypeId());
                    List<Subject> subjects = subjectMapper.selectList(subjectQueryWrapper);
                    subJectDto.setChildren(subjects);
                    return subJectDto;
                })
                .collect(Collectors.toList());
    }


}
